#include"util.hpp"
#include"level.hpp"
#include"message.hpp"
#include"format.hpp"
#include"sink.hpp"
#include"logger.hpp"
#include"buffer.hpp"
#include<string.h>


int main()
{
    // cout<<bitlog::util::date::now()<<endl;
    // string name="a/b/c/log.txt";
    // cout<<bitlog::util::file::exists(name)<<endl;
    // string path=bitlog::util::file::path(name);
    // bitlog::util::file::create_directory(path);
    // cout<<bitlog::util::file::exists(path)<<endl;
    
    //日志等级类测试
    // cout<<bitlog::LogLevel::tostring(bitlog::LogLevel::value::DEBUG)<<endl;
    // cout<<bitlog::LogLevel::tostring(bitlog::LogLevel::value::INFO)<<endl;
    // cout<<bitlog::LogLevel::tostring(bitlog::LogLevel::value::WARNING)<<endl;
    // cout<<bitlog::LogLevel::tostring(bitlog::LogLevel::value::ERROR)<<endl;
    // cout<<bitlog::LogLevel::tostring(bitlog::LogLevel::value::FATAL)<<endl;
    // cout<<bitlog::LogLevel::tostring(bitlog::LogLevel::value::OFF)<<endl;

    //日志标准输出类测试
    // bitlog::LogMsg msg("root","测试文件",12,"日志标准输出类测试",bitlog::LogLevel::value::INFO);
    // bitlog::Formatter formatter;
    // string str=formatter.format(msg);
    // cout<<str<<endl;

    //日志落地类测试
    // bitlog::LogMsg msg("root","测试文件",12,"日志标准输出类测试",bitlog::LogLevel::value::INFO);
    // bitlog::Formatter formatter;
    // string str=formatter.format(msg);
    // bitlog::LogSink::ptr stdout_lsp= bitlog::SinkFactor::create<bitlog::StdoutSink>();
    // bitlog::LogSink::ptr file_lsp= bitlog::SinkFactor::create<bitlog::FileSink>("./logfile/test.log");
    // bitlog::LogSink::ptr rollbysize_lsp= bitlog::SinkFactor::create<bitlog::RollBysizeSink>("./logfile/test.log-",1024*1024);
    // stdout_lsp->log(str.c_str(),str.size());
    // file_lsp->log(str.c_str(),str.size());
    // size_t cursize=0;
    // while(cursize<1024*1024*10){
    //     rollbysize_lsp->log(str.c_str(),str.size());
    //     cursize+=str.size();
    // }

    //logger类测试
    // string logger_name="sync_logger";
    // bitlog::LogLevel::value limit=bitlog::LogLevel::value::WARNING;
    // bitlog::Formatter::ptr fmt(new bitlog::Formatter());
    // bitlog::LogSink::ptr stdout_lsp= bitlog::SinkFactor::create<bitlog::StdoutSink>();
    // bitlog::LogSink::ptr file_lsp= bitlog::SinkFactor::create<bitlog::FileSink>("./logfile/test.log");
    // bitlog::LogSink::ptr rollbysize_lsp= bitlog::SinkFactor::create<bitlog::RollBysizeSink>("./logfile/test.log-",1024*1024);
    // vector<bitlog::LogSink::ptr> sinks={stdout_lsp,file_lsp,rollbysize_lsp};
    // bitlog::Logger::ptr logger(new bitlog::SyncLogger(logger_name,limit,fmt,sinks));
    //-----------------------------------------------------------------------------------------------------------------------
    //通过建造者类来完成日志创建
    // bitlog::Bulider::ptr builder(new bitlog::LocalLoggerBuilder());
    // builder->buildLoggerName("sync_logger");
    // builder->buildLoggerLevel(bitlog::LogLevel::value::WARNING);
    // // builder->buildFormatter();
    // builder->buildLoggerType(bitlog::Logger::Type::LOGGER_SYNC);
    // builder->buildSinks<bitlog::StdoutSink>();
    // builder->buildSinks<bitlog::FileSink>("./logfile/test.log");
    // bitlog::Logger::ptr logger=builder->build();
    // logger->debug(__FILE__,__LINE__,"%s","测试日志");
    // logger->info(__FILE__,__LINE__,"%s","测试日志");
    // logger->warn(__FILE__,__LINE__,"%s","测试日志");
    // logger->error(__FILE__,__LINE__,"%s","测试日志");
    // logger->fatal(__FILE__,__LINE__,"%s","测试日志");
    // string str="测试日志";
    // size_t cursize=0;
    // while(cursize<(1024*1024-str.size())){
    //     logger->fatal(__FILE__,__LINE__,"%s","测试日志");
    //     cursize+=str.size();
    // }

    //测试buffer类---异步缓冲区类的测试
    //方法：准备一个比较大的原文件，然后将里面的内容读取到缓冲区中，然后再将缓冲区中内容写入到一个新的文件中，对比一下两个文件的内容是否一致即可
    std::ifstream ifs("./logfile/test.log",ios::binary);
    if(ifs.is_open()==false) {
        cout<<"原文件打开失败\n";
        return -1;
    }
    ifs.seekg(0,ios::end);   //读写位置跳转到文件末尾
    size_t fsize=ifs.tellg();  //读写位置相较于文件起始位置的偏移量
    ifs.seekg(0,ios::beg);   //读写位置跳回到文件开始
    string body;
    body.resize(fsize);
    ifs.read(&body[0],fsize);
    if(ifs.good()==false){
        cout<<"读取失败\n";
        return -1;
    }
    ifs.close();
    bitlog::Buffer buffer;
    std::ofstream ofs("./logfile/tmp.log",ios::binary);
    if(ofs.is_open()==false){
        cout<<"创建打开新文件失败\n";
        return -1;
    }
    for(int i=0;i<body.size();i++){
        ofs.write(&body[i],1);
    }
    ofs.close();

    return 0;
}